package com.wr.controller.comment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.wr.cmmon.UserContext;
import com.wr.model.Comment;
import com.wr.model.User;
import com.wr.service.comment.CommentService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author weirong
 * @Title:
 * @Description:
 * @date 2018/6/1315:39
 */
@Controller
@RequestMapping("comment")
public class CommentController {

    Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private CommentService commentService;
    @Autowired
    private RedisTemplate redisTemplate;

    private int MAX_ALLOWED_TIMES = 5;

    @RequestMapping("add")
    @ResponseBody
    public HashMap<String,Object> add(Comment comment) {

        HashMap<String,Object> map = Maps.newHashMap();

        if(StringUtils.isBlank(comment.getContent())) {
            map.put("code",1);
            map.put("msg","评论内容不能为空");
            return map;
        }

        if(comment.getArtid() == null) {
            map.put("code",1);
            map.put("msg","评论文章不存在");
            return map;
        }
        User user = UserContext.getUser();
        comment.setUserid(user.getId());
        comment.setUsername(user.getUsername());

        String key = "add_comment_limit:"+user.getId()+":"+comment.getArtid();
        Object o = redisTemplate.opsForValue().get(key);
        String count =  String.valueOf(redisTemplate.opsForValue().get(key));
        if("null" != count && StringUtils.isNotBlank(count)) {
            if(Integer.parseInt(count) > MAX_ALLOWED_TIMES) {
                map.put("code",1);
                map.put("msg","1分钟内只能评论5次");
                return map;
            }
        }
        else {
            redisTemplate.opsForValue().set(key,0,1,TimeUnit.MILLISECONDS);
        }
        try {
            commentService.add(comment);
            redisTemplate.opsForValue().increment(key,1);
            map.put("code",0);
            map.put("msg","评论成功");
            return map;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            map.put("code",1);
            map.put("msg","系统异常");
            return map;
        }
    }

    @PostMapping("jsonList")
    @ResponseBody
    public List<Comment> jsonList(Integer pageNo,Integer artid) {

        JSONObject jsonObject = commentService.listByArtId(artid,pageNo);
        JSONArray rows = jsonObject.getJSONArray("rows");
        List<Comment> commentList = JSONArray.parseArray(JSON.toJSONString(rows),Comment.class);

        return commentList;
    }

}
